package com.hceax.servlet;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hceax.servlet.UserDAO;
import com.hceax.servlet.User;

@WebServlet(name = "LoginServlet",urlPatterns = {"/login.do"})
public class LoginServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1186383425313386137L;
	static UserDAO userDAO = new UserDAO();
	
	public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
		String sno = request.getParameter("sno");
		String password = request.getParameter("password");
		User user = userDAO.findUser(sno);
		response.setContentType("text/html;charset = UTF-8");
		//System.out.println(user.getSno()+"\n"+user.getPassword());
		if(sno.equals(user.getSno())&&password.equals(user.getPassword())) {
		//if(sno.equals("123")&&password.equals("123")) {
			request.setAttribute("OK", "欢迎你！");
			request.setAttribute("username", user.getUsername());
			RequestDispatcher rd = request.getRequestDispatcher("/test.jsp");
			rd.forward(request, response);
		}else {
			request.setAttribute("back", "用户名或密码不正确！");
			RequestDispatcher rd = request.getRequestDispatcher("/login.jsp");
			rd.forward(request, response);
		}
	}
}
